package com.media.rabbitmq.consumer;

import com.common.handler.ExchangeServiceHandler;
import com.media.service.IMediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 媒资服务消费者
 */
@Component
@Slf4j
public class MediaConsumer {

    @Resource
    IMediaService iMediaService;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "media-remover-id-queue" , durable = "true" , arguments = @Argument(name = "x-queue-mode" , value = "lazy")), // 持久化队列 开启惰性队列
            exchange = @Exchange(name = ExchangeServiceHandler.MEDIA_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = "media.remove.id"
    ))
    public void removeMediaById(Long id) {
        log.info("媒资服务：收到删除媒体信息消息，id:{}",id);
        iMediaService.deleteById(id);
    }

    /**
     * 传递一个id集合 删除对应的文件信息
     * @param ids 文件id集合
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "media-remove-ids-queue" , durable = "true" , arguments = @Argument(name = "x-queue-mode" , value = "lazy")), // 持久化队列 开启惰性队列
            exchange = @Exchange(name = ExchangeServiceHandler.MEDIA_EXCHANGE , type = ExchangeTypes.DIRECT),
            key = "media.remove.ids"
    ))
    public void removeMediaByIds(List<Long> ids) {
        log.info("媒资服务：收到删除媒体信息消息，ids:{}",ids);
        iMediaService.deleteByIds(ids);
    }
}
